package com.ouc.openplatform.ktsy.task;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
public class NJ_GetOrdersToOracle {
	public Result run(Job job) throws Throwable { 
		try {
			System.setProperty("jdbc.drivers", "oracle.jdbc.OracleDriver");
			String url="jdbc:oracle:thin:@10.136.115.82:1521:ORCL";
			String username="order2016";
			String password="ouccs";
			Connection con = DriverManager.getConnection(url,username,password);
			Statement st = con.createStatement();
			//String sql = "insert into NJ_VIEW_ORDER(ID,WORK_OPERATIONOUTMAINITEMCODE) values(11111,'内销')";
			//int rs = st.executeUpdate(sql);
			//System.out.println(rs+"----------");
			List<NJ_VIEW_ORDER> orderList=NJ_GetOrdersFromSqlServer.nj_Getdata();
			for (NJ_VIEW_ORDER nj_VIEW_ORDER : orderList) {
				
				String ocode = nj_VIEW_ORDER.getWorkOperationoutmainitemcode();
				int oqty = nj_VIEW_ORDER.getWorkOperationoutmainitemqty();
				String oday = nj_VIEW_ORDER.getWorkuserDay();
				String obaoxiao = nj_VIEW_ORDER.getWorkuserBaoxiao();
				String omordercode = nj_VIEW_ORDER.getWorkuserMordercode();
				String olinename = nj_VIEW_ORDER.getWorkuserLinename();
				String olinecode = nj_VIEW_ORDER.getWorkuserLinecode();
				String oscript = nj_VIEW_ORDER.getProductdescription();
				
				String check1 = "select * from NJ_VIEW_ORDER where WORKUSER_MORDERCODE="+omordercode;
				ResultSet rs1 = st.executeQuery(check1);
				//String insert = String.format("insert into NJ_VIEW_ORDER(ID,WORK_OPERATIONOUTMAINITEMCODE,WORK_OPERATIONOUTMAINITEMQTY,WORKUSER_DAY,WORKUSER_BAOXIAO,WORKUSER_MORDERCODE,WORKUSER_LINENAME,WORKUSER_LINECODE,PRODUCTDESCRIPTION) values(SEQORDER.nextVal,'%s',%d,'%s','%s','%s','%s','%s','%s')",ocode,oqty,oday,obaoxiao,omordercode,olinename,olinecode,oscript);
				//System.out.println(insert);
				if(rs1.next()==false)
				{
					String insertorder = String.format("insert into NJ_VIEW_ORDER(ID,WORK_OPERATIONOUTMAINITEMCODE,WORK_OPERATIONOUTMAINITEMQTY,WORKUSER_DAY,WORKUSER_BAOXIAO,WORKUSER_MORDERCODE,WORKUSER_LINENAME,WORKUSER_LINECODE,PRODUCTDESCRIPTION) values(SEQORDER.nextVal,'%s',%d,'%s','%s','%s','%s','%s','%s')",ocode,oqty,oday,obaoxiao,omordercode,olinename,olinecode,oscript);
					st.executeUpdate(insertorder);
					System.out.println("update ORDER "+omordercode+"----------");
				}
				
				//String sql = "insert into NJ_VIEW_ORDER(ID,WORK_OPERATIONOUTMAINITEMCODE) values(11111,'sxl')";
				//int rs = st.executeUpdate(sql);
				//System.out.println(rs+"----------");
			}
			System.out.println("update ORDER successful =.=\n");
			List<NJ_VIEW_ORDERDETAIL> orderListdetail=NJ_GetOrdersFromSqlServer.nj_Getdatadetail();
			for (NJ_VIEW_ORDERDETAIL nj_VIEW_ORDERDETAIL : orderListdetail) {
				
				String obarcode = nj_VIEW_ORDERDETAIL.getWorkuserBarcode();
				String ocode = nj_VIEW_ORDERDETAIL.getWorkOperationoutmainitemcode();
				int oqty = nj_VIEW_ORDERDETAIL.getWorkOperationoutmainitemqty();
				String oday = nj_VIEW_ORDERDETAIL.getWorkuserDay();
				String obaoxiao = nj_VIEW_ORDERDETAIL.getWorkuserBaoxiao();
				String omordercode = nj_VIEW_ORDERDETAIL.getWorkuserMordercode();
				String olinename = nj_VIEW_ORDERDETAIL.getWorkuserLinename();
				String olinecode = nj_VIEW_ORDERDETAIL.getWorkuserLinecode();
				String odes = nj_VIEW_ORDERDETAIL.getWorkuserRightmostitemname();
				
				String check2 = "select * from NJ_VIEW_ORDERDETAIL where WORKUSER_BARCODE='"+obarcode+"'";
				ResultSet rs2 = st.executeQuery(check2);
				//String insert = String.format("insert into NJ_VIEW_ORDER(ID,WORK_OPERATIONOUTMAINITEMCODE,WORK_OPERATIONOUTMAINITEMQTY,WORKUSER_DAY,WORKUSER_BAOXIAO,WORKUSER_MORDERCODE,WORKUSER_LINENAME,WORKUSER_LINECODE,PRODUCTDESCRIPTION) values(SEQORDER.nextVal,'%s',%d,'%s','%s','%s','%s','%s','%s')",ocode,oqty,oday,obaoxiao,omordercode,olinename,olinecode,oscript);
				//System.out.println(insert);
				if(rs2.next()==false)
				{
					String insertorderdetail = String.format("insert into NJ_VIEW_ORDERDETAIL(ID,WORKUSER_BARCODE,WORK_OPERATIONOUTMAINITEMCODE,WORK_OPERATIONOUTMAINITEMQTY,WORKUSER_DAY,WORKUSER_BAOXIAO,WORKUSER_MORDERCODE,WORKUSER_LINENAME,WORKUSER_LINECODE,WORKUSER_RIGHTMOSTITEMNAME) values(SEQORDERDETAIL.nextVal,'%s','%s',%d,'%s','%s','%s','%s','%s','%s')",obarcode,ocode,oqty,oday,obaoxiao,omordercode,olinename,olinecode,odes);
					st.executeUpdate(insertorderdetail);
					System.out.println("update ORDERDETAIL "+obarcode+"----------");
				}
				
				//String sql = "insert into NJ_VIEW_ORDER(ID,WORK_OPERATIONOUTMAINITEMCODE) values(11111,'sxl')";
				//int rs = st.executeUpdate(sql);
				//System.out.println(rs+"----------");
			}
			System.out.println("update ORDERDETAIL successful =.=\n");
			st.close();
			con.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Result successResult = new Result(Action.EXECUTE_SUCCESS, "成功提示信息");
		return successResult;
	}

}
